package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.model.StatisticsInfo;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品概况统计 前端控制器
 * </p>
 *
 * @author Bruce
 */
@RestController
@RequestMapping("/sys/collect")
@Api(description = "产品概况统计 ")
public class CommondityStatisticsController extends BaseController<CommondityStatisticsService, CommondityStatistics> {

    @Autowired
    private CommondityStatisticsService commondityStatisticsService;

    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IZslBrowseHistoryService iZslBrowseHistoryService;


    @Autowired
    private IZslFollowService iZslFollowService;

    @Autowired
    private IOriginAuthorizationService iOriginAuthorizationService;

    @Autowired
    private IZslOriginService iZslOriginService;


    @ApiOperation("对登录用户下产品进行统计更新")
//    @IgnoreAuth
    @RequestMapping(value = "/statistics", method = RequestMethod.POST)
    public ReturnObject<CommondityStatistics> statistics() {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        Long storeId = sysUser.getStoreId();
        CommondityStatistics commondityStatistics = commondityStatisticsService.selectOne(new EntityWrapper<CommondityStatistics>().eq("user_id",
                sysUser.getId()));
        if (commondityStatistics == null) {
            commondityStatistics = new CommondityStatistics();
        }

        //产品浏览数
        List<ZslBrowseHistory> histories = iZslBrowseHistoryService.selectList(new EntityWrapper<ZslBrowseHistory>().eq(
                "store_id",
                storeId));
        int commodityViews = 0;
        if (histories != null && histories.size() > 0) {
            commodityViews = histories.size();
        }
        commondityStatistics.setCommondityViews(commodityViews);
        //店铺关注数
        List<ZslFollow> follows = iZslFollowService.selectList(new EntityWrapper<ZslFollow>().eq(
                "store_id",
                storeId));
        int storeViews = 0;
        if (follows != null && follows.size() > 0) {
            storeViews = follows.size();
        }
        commondityStatistics.setStoreViews(storeViews);
        //授权品牌
        List<Object> ids = iOriginAuthorizationService.selectObjs(new EntityWrapper<OriginAuthorization>().eq(
                "store_id",
                storeId).eq("status", 1).setSqlSelect("origin_id"));
        if (ids != null && ids.size() > 0) {

            commondityStatistics.setBrandAuthorize(ids.size());
        } else {
            commondityStatistics.setBrandAuthorize(0);
        }
        //上架商品
        List<ZslStoreCommodity> inSales = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
                "store_id",
                storeId).eq("state", 0));
        int commodityInSale = 0;
        if (inSales != null && inSales.size() > 0) {
            commodityInSale = inSales.size();
        }
        commondityStatistics.setCommondityInSale(commodityInSale);
        //下架商品
        List<ZslStoreCommodity> outSales = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
                "store_id",
                storeId).eq("state", 1).eq("status", 0));
        int commodityOutSale = 0;
        if (outSales != null && outSales.size() > 0) {
            commodityOutSale = outSales.size();
        }
        commondityStatistics.setCommondityOutSale(commodityOutSale);
        //草稿箱产品

        List<ZslStoreCommodity> inDrafts = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
                "store_id",
                storeId).eq("status", 1));
        int commodityInDrafts = 0;
        if (inDrafts != null && inDrafts.size() > 0) {
            commodityInDrafts = inDrafts.size();
        }
        commondityStatistics.setCommondityInDrafts(commodityInDrafts);
//        //待审核
//        List<ZslStoreCommodity> toAudits = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
//                "user_id", BaseContextHandler.getUserID()).eq("is_agree", 1));
//        int commodityToAudit = 0;
//        if (toAudits != null && toAudits.size() > 0) {
//            commodityToAudit = toAudits.size();
//        }
//        commondityStatistics.setCommondityToAudit(commodityToAudit);
        //未通过

//        List<ZslStoreCommodity> refuseds = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
//                "user_id", BaseContextHandler.getUserID()).eq("is_agree", 2));
//        int commondityRefused = 0;
//        if (refuseds != null && refuseds.size() > 0) {
//            commondityRefused = refuseds.size();
//        }
//        commondityStatistics.setCommondityRefused(commondityRefused);
        //总商品数

        List<Integer> statuslist = new ArrayList<Integer>();
        statuslist.add(0);
        statuslist.add(1);

        List<ZslStoreCommodity> totals = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
                "store_id",
                storeId).in("status", statuslist));
        int commondityTotal = 0;
        if (totals != null && totals.size() > 0) {
            commondityTotal = totals.size();
        }
        commondityStatistics.setCommondityTotal(commondityTotal);
        //店铺曝光量
        List<Integer> weightlist = new ArrayList<Integer>();
        weightlist.add(1);
        weightlist.add(2);
        List<ZslStoreCommodity> exposures = iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq(
                "store_id",
                storeId).eq("state", 0).eq("status", 0).in("weight", weightlist));

        int storeExposures = 0;
        if (exposures != null && exposures.size() > 0) {
            storeExposures = exposures.size();
        }
        commondityStatistics.setStoreExposureNumber(storeExposures);
        commondityStatisticsService.updateById(commondityStatistics);
        commondityStatistics = commondityStatisticsService.selectByUserId(sysUser.getId());
        return new ReturnObject<>(0, "获取成功", commondityStatistics);
    }


}

